//
//  OrderDetailViewController.m
//  FactorySale
//
//  Created by 主事丫环 on 2020/1/7.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "WeiFaHuoOrderDetailModel.h"
#import "JiHuoGuanLiYuanAlertView.h"
#import "HXKOrderDetailCell.h"// 订单详情cell
#import "ShengChengFaHuoDanViewController.h" // 生成发货单

@interface OrderDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) UIView *tabFootView;
@property (nonatomic, strong) UILabel *timelab;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *addreddLab;
@property (nonatomic, strong) UILabel *phoneLab;
@property (nonatomic, strong) WeiFaHuoOrderDetailModel *detailModel;
@property (nonatomic, strong) NSMutableArray *partsArray;
@property (nonatomic, strong) JiHuoGuanLiYuanAlertView *alertView;
@property (nonatomic, strong) UIButton *jiedanBut;
@property (nonatomic, strong) UIButton *createBut;

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    //ShowToastWithText(@"加载中");
    [self createOrderDetailUI];
    [self requestGetUnSendPartsOrdersInfo];
}

-(void)requestGetUnSendPartsOrdersInfo{
    NSDictionary *param = @{
        @"id":NOTNIL(self.id),//产品种类id
        @"orderId":NOTNIL(self.orderId)//    订单Id
    };
    [NetWorkTool POST:getUnSendPartsOrdersInfo param:param success:^(id dic) {
        self.detailModel = [WeiFaHuoOrderDetailModel mj_objectWithKeyValues:dic[@"data"]];
        self.partsArray = [WeiFaHuoDetailPartsListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"parts"]];
        [self refreshViewsData];
        [self.mytableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

#pragma mark - 接单
-(void)jiedanAction:(UIButton *)but{
    //接单
    KMyLog(@"您点击了 订单详情下面的 接单按钮");
    NSDictionary *param = @{
        @"id":NOTNIL(self.id),//产品种类id
        @"orderId":NOTNIL(self.orderId)//    订单Id
    };
    [NetWorkTool POST:receiveOrder param:param success:^(id dic) {
        [self showJiHuoHuiYuanAlertView];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

//弹出 接单成功view
-(void)showJiHuoHuiYuanAlertView {
    if (_alertView) {
        [_alertView removeFromSuperview];
    }
    _alertView = [[JiHuoGuanLiYuanAlertView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    [_alertView.activeBtn addTarget:self action:@selector(goActiveAdminVC) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
}

-(void)goActiveAdminVC {
    [_alertView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)refreshViewsData {
    //isSubFactory     是否接单 0否1 是
    NSString *isSubFactory = [NSString stringWithFormat:@"%@",self.detailModel.isSubFactory];
    if ([isSubFactory isEqualToString:@"1"]) {
        self.jiedanBut.hidden = YES;
        self.createBut.hidden = NO;
    }
    //下单时间
    self.timelab.text = [NSString stringWithFormat:@"%@",self.detailModel.createTime];
    //厂家名称
    self.titleLab.text = self.detailModel.name;
    //地址lab
    self.addreddLab.text = [NSString stringWithFormat:@"收货地址：%@",self.detailModel.address];
    //厂家 电话号
    self.phoneLab.text = [NSString stringWithFormat:@" %@", self.detailModel.phone];
}

- (void)createOrderDetailUI {
    
    UIView *UpView = [[UIView alloc] initWithFrame:CGRectMake(15, kNaviHeight + 10, KWIDTH- 30, 185)];
    UpView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:UpView];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UpView.width, UpView.height)];
    [imageV setImage:imgname(@"OrderDetailBg")];
    [UpView addSubview:imageV];
    
    UILabel *timelab = [[UILabel alloc] initWithFrame:CGRectMake(9, 9, KWIDTH-30-18, 12)];
    timelab.font = FontSize(13);
    timelab.textColor = [UIColor whiteColor];
    [imageV addSubview:timelab];
    self.timelab = timelab;
    
    UILabel *topline = [[UILabel alloc] initWithFrame:CGRectMake(9, timelab.bottom + 9, KWIDTH-30-18, 1)];
    topline.backgroundColor = [UIColor whiteColor];
    [imageV addSubview:topline];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(9, topline.bottom +12, KWIDTH-30-18, 13)];
    titleLab.font = FontSize(13);
    titleLab.textColor = [UIColor whiteColor];
    [imageV addSubview:titleLab];
    self.titleLab = titleLab;
    
    UILabel *addreddLab = [[UILabel alloc] initWithFrame:CGRectMake(9, titleLab.bottom +10, KWIDTH-30-18, 60)];
    addreddLab.font = FontSize(12);
    addreddLab.textColor = [UIColor whiteColor];
    addreddLab.numberOfLines = 3;
    [imageV addSubview:addreddLab];
    self.addreddLab = addreddLab;
    
    UILabel *bottomline = [[UILabel alloc] initWithFrame:CGRectMake(9, addreddLab.bottom + 5, KWIDTH-30-18, 1)];
    bottomline.backgroundColor = [UIColor whiteColor];
    [imageV addSubview:bottomline];
    
    UIImageView *phoneImg = [[UIImageView alloc] initWithFrame:CGRectMake((KWIDTH-30)/3, bottomline.bottom +5, (KWIDTH-30)/3+10, 36)];
    [phoneImg setImage:imgname(@"PhoneBg")];
    phoneImg.userInteractionEnabled = YES;
    [UpView addSubview:phoneImg];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneClick:)];
    [phoneImg addGestureRecognizer:tap];
    phoneImg.userInteractionEnabled = YES;
    
    UILabel *phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(28, 12, 95, 13)];
    phoneLab.text = @"";
    phoneLab.font = FontSize(13);
    phoneLab.textColor = [UIColor orangeColor];
    [phoneImg addSubview:phoneLab];
    self.phoneLab = phoneLab;
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 56)];
    
    _mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, UpView.bottom +30, KWIDTH, KHEIGHT-kNaviHeight-UpView.height-2) style:UITableViewStylePlain];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    _mytableView.tableFooterView = self.tabFootView;
    [self.view addSubview:_mytableView];
    
    if ([self.isSubFactory isEqualToString:@"0"]) {
        [self showtabFootView];
    } else {
        [self showtabFootViewFahuo];
    }
}

-(void)phoneClick:(UITapGestureRecognizer *)tap{
    
    [HFTools callMobilePhone:[self filterSpaces:self.phoneLab.text]];
}
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
-(void)showtabFootView {
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 116)];
    
    _jiedanBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_jiedanBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_jiedanBut setImage:imgname(@"JiedanBg") forState:UIControlStateNormal];
    _jiedanBut.frame  = CGRectMake(80, 50, KWIDTH - 160, 44);
    _jiedanBut.layer.masksToBounds = YES;
    _jiedanBut.layer.cornerRadius = 22;
    [_jiedanBut addTarget:self action:@selector(jiedanAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tabFootView addSubview: _jiedanBut];
    
    _mytableView.tableFooterView = self.tabFootView;
}

-(void)showtabFootViewFahuo {
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 116)];
    
    _createBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_createBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_createBut setTitle:@"生成发货单" forState:UIControlStateNormal];
    _createBut.titleLabel.font = FontSize(15);
    [_createBut setBackgroundImage:imgname(@"按钮背景") forState:UIControlStateNormal];
    _createBut.frame  = CGRectMake(80, 50, KWIDTH - 160, 44);
    _createBut.layer.masksToBounds = YES;
    _createBut.layer.cornerRadius = 22;
    [_createBut addTarget:self action:@selector(createOrderAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tabFootView addSubview: _createBut];
    
    _mytableView.tableFooterView = self.tabFootView;
}

//生成发货单 按钮 点击事件
- (void)createOrderAction:(UIButton *)button {
    ShengChengFaHuoDanViewController *vc = [[ShengChengFaHuoDanViewController alloc]init];
    vc.id = self.id;
    vc.orderId = self.orderId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.partsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    HXKOrderDetailCell *cell = [HXKOrderDetailCell cellWithTableView:tableView];
    WeiFaHuoDetailPartsListModel *listModel  = [WeiFaHuoDetailPartsListModel mj_objectWithKeyValues:[self.partsArray safeObjectAtIndex:indexPath.row]];
    cell.model = listModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"您点击了第 %ld 个分区 第%ld行Cell", indexPath.section, indexPath.row);
}

@end
